package com.lemon.springcloud.core.util;

import org.springframework.core.io.UrlResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.net.URL;
import java.util.*;

/**
 * Class Name: BuildInUtils
 * Create Date: 18-3-22 下午1:53
 * Creator: Chim·Zigui
 * Version: v1.0
 * Updater:
 * Date Time:
 * Description:
 */
public final class BuildInUtils {

    private static final String MOP_BOOT_BUILDIN_RESOURCE_LOCATION = "META-INF/lemon-default-config.properties";

    public static String getBuildInProps(String key, ClassLoader classLoader) {
        try {
            Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(MOP_BOOT_BUILDIN_RESOURCE_LOCATION) :
                    ClassLoader.getSystemResources(MOP_BOOT_BUILDIN_RESOURCE_LOCATION));
            String value = null;
            while (urls.hasMoreElements()) {
                URL url = urls.nextElement();
                Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
                value = properties.getProperty(key);
            }
            return value;
        } catch (IOException ex) {
            throw new IllegalArgumentException("Unable to load [" + key + "] configurations" +
                    " from location [" + MOP_BOOT_BUILDIN_RESOURCE_LOCATION + "]", ex);
        }
    }

}
